import 'package:flutter/material.dart';

class ToolsList extends StatefulWidget {
  final List toolsList;
  final void Function(dynamic item) onclick;
  const ToolsList({Key key, this.toolsList, this.onclick}) : super(key: key);

  @override
  _ToolsListState createState() => _ToolsListState();
}

class _ToolsListState extends State<ToolsList> {
  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromRGBO(248, 248, 249, 1),
      padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
      child: ListView(
        children: getToolsList(widget.toolsList),
      ),
    );
  }

  // 获取菜单项widget列表
  List<Widget> getToolsList(List tools) {
    return tools
        .asMap()
        .keys
        .map<Widget>((index) => createToolsItem(tools[index], index))
        .toList();
  }

  // 生成单个菜单项
  Widget createToolsItem(item, index) {
    return GestureDetector(
      onTap: () {
        widget.onclick({'item': item, 'index': index});
      },
      child: Container(
        width: double.infinity,
        height: 76,
        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: 1,
              color: Color.fromRGBO(229, 229, 229, 1),
            ),
          ),
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Column(
                children: <Widget>[
                  Container(
                    alignment: Alignment.topLeft,
                    child: Text(
                      item.planQuantity == 1 ? item.code : '多个',
                      style: TextStyle(
                        fontSize: 14,
                        height: 20 / 14,
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      alignment: Alignment.bottomLeft,
                      child: Text(
                        item.largeName + ' > ' + item.subName,
                        style: TextStyle(
                          fontSize: 12,
                          height: 16 / 12,
                          color: Color.fromRGBO(150, 151, 154, 1),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              alignment: Alignment.centerRight,
              child: Text(
                item.finishQuantity.toString() +
                    '/' +
                    item.planQuantity.toString(),
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                  height: 21 / 18,
                  color: Color.fromRGBO(89, 131, 252, 1),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
